﻿// -------------------------------------------------------------------------
//    @FileName         :    NFTransMapMsg.cpp
//    @Author           :    Gao.Yi
//    @Date             :    2025-09-10
//    @Email			:    445267987@qq.com
//    @Module           :    NFTransMapMsg
//    @Desc             :
//
// -------------------------------------------------------------------------

#include "NFTransMapMsg.h"

#include "NFMapPart.h"
#include "NFMapUtil.h"
#include "NFPacketSendUtil.h"
#include "DescStore/CityMapDesc.h"
#include "NFServerComm/NFServerCommon/NFIServerMessageModule.h"
#include "NFPlayer.h"
#include "proto_svr_common.nanopb.h"

NFTransMapMsg::NFTransMapMsg()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFTransMapMsg::~NFTransMapMsg()
{
}

int NFTransMapMsg::CreateInit()
{
    m_bTransMap = false;
    return 0;
}

int NFTransMapMsg::ResumeInit()
{
    return 0;
}

int NFTransMapMsg::StartGCMapMsgReq(int iMapId, const proto_ff::Proto_CSReq& csReq, const proto_ff::ExtraLevelEntrancePara* pData)
{
    auto pPlayer = GetPlayer();
    CHECK_NULL(0, pPlayer);

    if (pData)
    {
        m_stPara.FromPb(*pData);
    }

    auto pCityMapDesc = CityMapDesc::Instance()->GetDesc(iMapId);
    CHECK_NULL_WF(pPlayer->GetRoleID(), pCityMapDesc, "get map desc:{} Failed!", iMapId);

    SetTransMap();

    proto_ff::Proto_GCMapMsgReq gcReq;
    gcReq.set_role_id(pPlayer->GetRoleID());
    gcReq.set_cmd(E_CS_MAP_QUERY_REQ);
    gcReq.mutable_cs_req()->CopyFrom(csReq);

    proto_ff::GCMapMsgReqData* pGCReqData = gcReq.mutable_req_data();
    pGCReqData->set_type(proto_ff::EN_MAP_MSG_ENTER_MAP);
    pGCReqData->mutable_enter_map()->set_map_id(iMapId);
    pGCReqData->mutable_enter_map()->set_index(0);
    pPlayer->MakeCityPlayerData(pGCReqData->mutable_enter_map()->mutable_role_info());

    pPlayer->SendTransToCityServer(E_GC_CITYSVR_MAPMSG_REQ, gcReq, GetGlobalId(), 0);
    return 0;
}

int NFTransMapMsg::SetTransMap()
{
    auto pPlayer = GetPlayer();
    CHECK_NULL(0, pPlayer);

    NFMapPart* pMapPart = NFMapUtil::GetMapPart(pPlayer);
    CHECK_NULL(pPlayer->GetRoleID(), pMapPart);

    pMapPart->SetInTransMap(true);
    return 0;
}

int NFTransMapMsg::HandleDispSvrRes(uint32_t nMsgId, const NFDataPackage& packet, uint32_t reqTransId, uint32_t rspTransId)
{
    int iRetCode = 0;
    switch (nMsgId)
    {
        case proto_ff::E_SG_ERROR:
            SetFinished(packet.nErrCode);
            break;
        case proto_ff::E_GC_CITYSVR_MAPMSG_RSP:
            iRetCode = ProcGCMapMsgRsp(nMsgId, packet, reqTransId, rspTransId);
            break;
        default:
        {
            LOG_ERR(0, iRetCode, "unexpected msg id:{}", nMsgId);
            iRetCode = -1;
            break;
        }
    }

    return iRetCode;
}

int NFTransMapMsg::OnTransFinished(int iRunLogicRetCode)
{
    auto pPlayer = GetPlayer();
    CHECK_NULL(0, pPlayer);

    if (m_bTransMap)
    {
        NFMapPart* pMapPart = NFMapUtil::GetMapPart(pPlayer);
        CHECK_NULL(pPlayer->GetRoleID(), pMapPart);
        pMapPart->SetInTransMap(false);
    }

    if (iRunLogicRetCode != 0)
    {
        if (m_stCSMsgHeadReq.m_iMsgID == E_CS_MAP_QUERY_REQ)
        {
            NFPacketSendUtil::SendErrToClient(pPlayer, E_CS_MAP_QUERY_REQ, (proto_ff::Proto_CS_ErrorCode)iRunLogicRetCode);
        }
    }
    return 0;
}

int NFTransMapMsg::ProcGCMapMsgRsp(uint32_t nMsgId, const NFDataPackage& packet, uint32_t reqTransId, uint32_t rspTransId)
{
    proto_ff::Proto_GCMapMsgRsp cCMapMsgRsp;
    CLIENT_MSG_PROCESS_WITH_PRINTF(packet, cCMapMsgRsp);

    int iRetCode = NFMapUtil::HandleGCMapMsgRsp(cCMapMsgRsp, m_stPara);
    CHECK_RET(iRetCode, "handle map msg rsp failed!");

    if (iRetCode == 0)
    {
        SetFinished(0);
    }

    return iRetCode;
}
